package library.Tools
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	import library.Function.GlobalFun;
	
	public class Flashfps extends Sprite
	{
		private var _numFrames:Number = 0;
		private var _interval:Number = 10
		private var _startTime:Number;
		private var _fpsText:TextField;
		
		public function Flashfps()
		{  
			_startTime = getTimer();
			_fpsText = new TextField();
			_fpsText.text = "loading";
			_fpsText.selectable = false;
			_fpsText.textColor = 0x33781f
			GlobalFun.setTextFormat(_fpsText);
			addChild(_fpsText);
			this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
		}
		
		private function onAdded(event:Event):void
		{
			this.addEventListener(Event.ENTER_FRAME, update, false);
		}
		
		private function onRemoved(event:Event):void
		{
			this.removeEventListener(Event.ENTER_FRAME,update,false)
		}
		
		private function update(event:Event):void
		{
			if (++_numFrames == _interval) {
				var now:Number = getTimer();
				var elapsedSeconds:Number = (now - _startTime) / 1000;
				var actualFPS:Number = _numFrames / elapsedSeconds;
				_fpsText.text = ("FPS:"+actualFPS.toFixed(2));
				_startTime = now;
				_numFrames = 0;
			}
		}
	}
}